package cn.uo86.bs.utils;

import cn.uo86.bs.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent.ShortcutIconResource;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;

public class Utils {
	private static final String TAG = "Utils";
	private static final boolean DEBUG =true;
	
	private final static int nofification_Id = 288;
	
	
	public static void logv(String tag, String log) {
  		Log.v(tag, "BookSearch: " + log);
  	}
  	
  	public static void logd(String tag, String log) {
  		Log.d(tag, "BookSearch: " + log);
  	}
  	
  	public static void logi(String tag, String log) {
  		Log.i(tag, "BookSearch: " + log);
  	}
  	
  	public static void logw(String tag, String log) {
  		Log.w(tag, "BookSearch: " + log);
  	}
  	
  	public static void loge(String tag, String log) {
  		Log.e(tag, "BookSearch: " + log);
  	}
  	
  	public static void loge(String tag, String msg, Throwable tr) {
  		Log.e(tag, "BookSearch: " + msg, tr);
  	}


	public final static String ISEXITTIP = "isExitTip";
	public final static String ISAPPMARK = "isAppMark"; 
	public final static String ISFIRST ="isFirst";
	
	public static void createShortCut(Activity activity){
		writeXmlBykey(ISFIRST,true,activity);
		
		Intent targetIntent = new Intent(activity.getBaseContext(),activity.getClass());
		Intent shortcutIntent = new Intent();
        shortcutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,ShortcutIconResource.fromContext(activity.getBaseContext(), R.drawable.ic_launcher));
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,activity.getString(R.string.app_name)); //activity.getString(R.string.app_name)
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,targetIntent);
        activity.sendBroadcast(shortcutIntent);
	}
	
	public static void markDialog(final Activity activity){
		AlertDialog.Builder builder0 = new Builder(activity);
		builder0.setMessage(activity.getString(R.string.app_name)+"是优质Android软件,\n只有为软件打分评论才能免费使用哟!\n谢谢您的支持!");
		builder0.setTitle("请给软件评分");
		builder0.setNegativeButton("五星支持", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				writeXmlBykey(ISAPPMARK,true,activity);
				openAppInMarket(activity.getPackageName(), activity.getBaseContext());
			}
		});
		builder0.create().show();
	}
	
	
	public static void exitDialog(final Activity activity) {
		  
		  AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		   builder.setTitle("退出确认");
		   CheckBox cb = new CheckBox(activity);
		   cb.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				writeXmlBykey(ISEXITTIP,true,activity);
			}
		});
		   cb.setText("不再提示(点击菜单键亦可评分和分享)");
		   builder.setMessage("您确定要退出吗?\n\n(如果您喜欢本应用,给我们好评并分享给您好友吧^-^)\n");
		   builder.setView(cb);
		   
		   builder.setPositiveButton("确定", new OnClickListener() {
		    
		    @Override
		    public void onClick(DialogInterface dialog, int which) {
		    	activity.finish();
		    }
		   });
		   builder.setNeutralButton("分享", new OnClickListener() {
		    
		    @Override
		    public void onClick(DialogInterface dialog, int which) {
		    	openAppShare(activity);
		    }
		   });
		   builder.setNegativeButton("五星支持",new OnClickListener() {
		    
		    @Override
		    public void onClick(DialogInterface dialog, int which) {
		    	openAppInMarket(activity.getPackageName(), activity.getBaseContext());
		    }
		   });
		   builder.show();
		  
		  
		  
		 }
	
	public static void openAppInMarket(String packName, Context context) {
		if ((context == null) || (packName == null)
				|| (packName.length() == 0))
			return;

		String str1 = "market://details?id=" + packName;
		try {
			Uri localUri1 = Uri.parse(str1);
			Intent intent1 = new Intent("android.intent.action.VIEW",
					localUri1);
			intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			Log.v("WindowControl", "intent"+intent1);
			context.startActivity(intent1);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void openAppShare(Activity activity){
		Intent intent=new Intent(Intent.ACTION_SEND); 
    	intent.setType("text/plain");  
    	intent.putExtra(Intent.EXTRA_SUBJECT, "分享"); 
    	intent.putExtra(Intent.EXTRA_TEXT, "我正在使"+activity.getString(R.string.app_name)+",推荐你也来体验一下,什么小说都可以搜到,相当方便market://details?id="+activity.getPackageName());  
    	activity.startActivity(Intent.createChooser(intent, "分享"));
	}
	
	public static Boolean readXmlBooleanByKey(String key,final Activity activity){
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(activity);  
		Boolean isTrue = settings.getBoolean(key, false);
		return isTrue;

	}
	
	public static void writeXmlBykey(String key,Boolean value,final Activity activity){
		Editor e = PreferenceManager.getDefaultSharedPreferences(activity).edit();  
		e.putBoolean(key, value);
		e.commit();
	}
}
